using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Application.Utils;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    public class AddStuScoresController : ControllerBase
    {
        private readonly IRepository<Students> _Students;
        private readonly IRepository<College> _College;
        private readonly IRepository<Major> _Major;
        private readonly IRepository<Avatar> _Avatar;
        private readonly IRepository<Class> _Class;
        private readonly IRepository<StuUsers> _StuUsers;
        private readonly IRepository<Mark> _Mark;

        public AddStuScoresController(IRepository<Mark> Mark, IRepository<StuUsers> StuUsers, IRepository<Avatar> Avatar, IRepository<Class> Class, IRepository<Major> Major, IRepository<College> College, IRepository<Students> Students)
        {
            _Mark = Mark;

            _StuUsers = StuUsers;

            _Avatar = Avatar;

            _Class = Class;

            _Major = Major;

            _College = College;

            _Students = Students;
        }

        //更新数据成绩
        [HttpPost("/GetUpScore")]
        public async Task<object> UpdStuScores(Mark marks)
        {

            var mark = _Mark.Table.FirstOrDefault(x => x.StudentId == marks.StudentId && x.Course == marks.Course);

            if (mark == null)
            {

                return new
                {

                    code = 400,

                    msg = "出错了",

                    data = ""


                };

            }

            mark.Score = marks.Score;

            try
            {
                await _Mark.UpdateAsync(mark);

                return new
                {

                    code = 200,

                    msg = "更新成功",

                    data = ""


                };

            }
            catch (System.Exception)
            {
                return new
                {

                    code = 400,

                    msg = "更新失败",

                    data = ""


                };

            }


        }

        //批量成绩录入
        [HttpPost("/GetAddStuScores")]
        public async Task<object> AddStuScores(List<Mark> marks)
        {

            var students = _Students.Table.ToList();
            var marks1 = _Mark.Table.ToList();

            List<object> list = new List<object>();

            foreach (var item in marks)
            {

                var student = students.FirstOrDefault(x => x.StudentId == item.StudentId);

                if (student == null)
                {


                    return new
                    {

                        code = "400",
                        data = "",
                        msg = "学号为：" + item.StudentId + "的同学不存在"

                    };

                }

                var studenta = marks1.FirstOrDefault(x => x.StudentId == item.StudentId && x.Course == item.Course);

                if (studenta != null)
                {
                    if (studenta.Score != item.Score)
                    {
                        var data = new
                        {

                            StudentId = studenta.StudentId,

                            Score = item.Score,

                            Course = studenta.Course,

                            StudentName = student.StudentName

                        };

                        list.Add(data);
                    }

                }
                else
                {
                    try
                    {
                        await _Mark.AddAsync(item);

                    }
                    catch
                    {

                        return new
                        {

                            code = "400",
                            data = "",
                            msg = "保存失败"

                        };
                    }
                }

            }

            if (list.Count > 0)
            {



                return new
                {

                    code = "355",
                    data = list,
                    msg = "存在重复录入"

                };


            }
            else
            {
                return new
                {

                    code = "200",
                    data = "",
                    msg = "保存成功"

                };

            }





        }


    }
}